{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一周扩展作业第2题目\n",
    "'''\n",
    " 创建第一个视觉程序“Hello，world！”，显示Lena图片\n",
    " '''\n",
    "import cv2\n",
    "img = cv2.imread(r'lena.jpg')\n",
    "cv2.imshow(\"hello world\", img)\n",
    "cv2.waitKey(10000)  # 10秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\nopencv将彩色图像由RGB颜色空间转换到HSV颜色空间后，按照色调、饱和度、亮度排列组织数据\\n'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一周扩展作业第3题目\n",
    "'''\n",
    "对Lena图像，分解得到RGB分量及HSV分量，显示各分量，并对结果进行比较说明\n",
    "'''\n",
    "import cv2\n",
    "import numpy as np\n",
    "img = cv2.imread(r'lena.jpg')\n",
    "width = int(img.shape[0])\n",
    "height = int(img.shape[1])\n",
    "# 显示原图\n",
    "cv2.imshow(\"source image\", img)\n",
    "# 显示红色分量、绿色分量、蓝色分量\n",
    "cv2.imshow(\"RED\", img[:, :, 2])\n",
    "cv2.imshow(\"GREEN\", img[:, :, 1])\n",
    "cv2.imshow(\"BLUE\", img[:, :, 0])\n",
    "cv2.waitKey(10000)  # 10秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "\n",
    "# 按照颜色显示红色分量、绿色分量、蓝色分量\n",
    "img_red = img.copy()\n",
    "zeros_matrix = np.matrix(np.zeros([width, height]))\n",
    "img_red[:, :, 0] = zeros_matrix\n",
    "img_red[:, :, 1] = zeros_matrix\n",
    "cv2.imshow(\"Red img\", img_red)\n",
    "img_green = img.copy()\n",
    "img_green[:, :, 0] = zeros_matrix\n",
    "img_green[:, :, 2] = zeros_matrix\n",
    "cv2.imshow(\"Green img\", img_green)\n",
    "img_blue = img.copy()\n",
    "img_blue[:, :, 1] = zeros_matrix\n",
    "img_blue[:, :, 2] = zeros_matrix\n",
    "cv2.imshow(\"Blue img\", img_blue)\n",
    "cv2.waitKey(10000)  # 10秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "'''\n",
    "opencv读取彩色图像，默认按照BGR排列组织数据\n",
    "'''\n",
    "\n",
    "hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "# 显示原图\n",
    "cv2.imshow(\"source image\", img)\n",
    "# 显示色调H、饱和度S、亮度\n",
    "cv2.imshow(\"Hue\", hsv[:, :, 0])\n",
    "cv2.imshow(\"Saturation\", hsv[:, :, 1])\n",
    "cv2.imshow(\"Value\", hsv[:, :, 2])\n",
    "cv2.waitKey(10000)  # 10秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "'''\n",
    "opencv将彩色图像由RGB颜色空间转换到HSV颜色空间后，按照色调、饱和度、亮度排列组织数据\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习高斯模糊、颜色空间转换和阈值化\n",
    "import cv2\n",
    "img = cv2.imread(r'lena.jpg')\n",
    "# 高斯模糊\n",
    "imgGauss = cv2.GaussianBlur(img, (5, 5), 0)\n",
    "# 缩放至一半大小\n",
    "img1 = cv2.resize(img, (int(img.shape[1]/2), int(img.shape[0]/2)))\n",
    "# 缩放至1/4大小\n",
    "img2 = cv2.pyrDown(img1)\n",
    "# 获取灰度图\n",
    "imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "# 阈值化，122\n",
    "_, imgGray1 = cv2.threshold(imgGray, 120, 0xff, cv2.THRESH_BINARY)\n",
    "# 显示原图\n",
    "cv2.imshow(\"source image\", img)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"gaussian filtered image\", imgGauss)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"half size image\", img1)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"1/4 image\", img2)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"gray image\", imgGray)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()\n",
    "cv2.imshow(\"threshold image\", imgGray1)\n",
    "cv2.waitKey(2000)  # 2秒后退出 或 按任意键退出\n",
    "cv2.destroyAllWindows()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
